package lesson02;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TestCalc {
    public static void main(String[] args) {
        new Calculator();
    }
}
class Calculator extends Frame {
    public Calculator(){
    TextField num1=new TextField(10);
    TextField num2=new TextField(10);
    TextField num3=new TextField(20);
    Button button=new Button("=");
    button.addActionListener(new MyCalculatorListener(num1,num2,num3));
    Label label1=new Label("+");
    setLayout(new FlowLayout());
    add(num1);
    add(label1);
    add(num2);
    add(button);
    add(num3);
    pack();
    setVisible(true);
    }
}
class MyCalculatorListener implements ActionListener{
    private TextField num1,num2,num3;
    public MyCalculatorListener(TextField num1,TextField num2,TextField num3){
        this.num1=num1;
        this.num2=num2;
        this.num3=num3;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
//        num3.setText("");
        //获取加数和被加数
        int n1=Integer.parseInt(num1.getText());
        int n2=Integer.parseInt(num2.getText());
        //进行计算并放入第三个框
        num3.setText(""+(n1+n2));
        //清除前两个框
        num1.setText("");
        num2.setText("");
    }
}
